package com.ego.goods.listener;

import com.ego.goods.service.GoodsService;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.File;
import java.util.Map;

@Component
public class GoodsListener {

    @Autowired
    private GoodsService goodsService;


    @RabbitListener(bindings = @QueueBinding(
            exchange = @Exchange(value = "ego.item.exchange",//指定交换机
                    ignoreDeclarationExceptions = "true",
                    type = ExchangeTypes.TOPIC//消息模型
            ),
            value = @Queue(value = "ego.delete.update.queue", durable = "true"
            ),//指定队列
            key = {"item.insert", "item.update"}//routingKey
    ))
    public void updateHTML(Long id) {
        if (id == null) {
            return;
        }
        Map<String, Object> model = this.goodsService.loadModel(id);
        this.goodsService.createStaticHtml(model, id);
    }

    @RabbitListener(bindings = @QueueBinding(
            exchange = @Exchange(value = "ego.item.exchange",//指定交换机
                    ignoreDeclarationExceptions = "true",
                    type = ExchangeTypes.TOPIC//消息模型
            ),
            value = @Queue(value = "ego.delete.update.queue", durable = "true"
            ),//指定队列
            key = {"item.delete"}//routingKey
    ))
    public void deleteHTML(Long id) {
        if (id == null) {
            return;
        }
        File file = new File("D:\\nginx-1.15.9\\html\\item\\" + id + ".html");
        if (!file.exists()) {
            return;
        }
        file.deleteOnExit();
    }

}
